package com.example.joblinker.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.joblinker.common.Result;
import com.example.joblinker.dto.JobFairDTO;
import com.example.joblinker.service.JobFairService;
import com.example.joblinker.util.JwtUtil;
import com.example.joblinker.vo.JobFairVO;
import com.example.joblinker.vo.CompanyVO;
import com.example.joblinker.vo.JobVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/jobfairs")
public class JobFairController {
    
    @Resource
    private JobFairService jobFairService;
    
    @Resource
    private JwtUtil jwtUtil;
    
    /**
     * 创建招聘会
     */
    @PostMapping
    public Result<JobFairVO> createJobFair(@RequestBody @Valid JobFairDTO jobFairDTO) {
        JobFairVO vo = jobFairService.createJobFair(jobFairDTO);
        return Result.success(vo);
    }
    
    /**
     * 更新招聘会
     */
    @PutMapping("/{id}")
    public Result<JobFairVO> updateJobFair(@PathVariable Long id, @RequestBody @Valid JobFairDTO jobFairDTO) {
        JobFairVO vo = jobFairService.updateJobFair(id, jobFairDTO);
        return Result.success(vo);
    }
    
    /**
     * 获取招聘会详情
     */
    @GetMapping("/{id}")
    public Result<JobFairVO> getJobFairDetail(@PathVariable Long id) {
        JobFairVO vo = jobFairService.getJobFairDetail(id);
        return Result.success(vo);
    }
    
    /**
     * 获取招聘会列表
     */
    @GetMapping
    public Result<Page<JobFairVO>> getJobFairList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String title,
            @RequestParam(required = false) Integer status) {
        Page<JobFairVO> result = jobFairService.getJobFairList(page, size, title, status);
        return Result.success(result);
    }
    
    /**
     * 取消招聘会
     */
    @PutMapping("/{id}/cancel")
    public Result<Void> cancelJobFair(@PathVariable Long id) {
        jobFairService.cancelJobFair(id);
        return Result.success();
    }
    
    /**
     * 用户报名招聘会
     */
    @PostMapping("/{id}/register")
    public Result<Boolean> registerForJobFair(@PathVariable Long id, HttpServletRequest request) {
        Long userId = jwtUtil.getUserIdFromRequest(request);
        boolean success = jobFairService.registerForJobFair(id, userId);
        return Result.success(success);
    }
    
    /**
     * 企业报名招聘会
     */
    @PostMapping("/{id}/company/register")
    public Result<Boolean> registerCompanyForJobFair(
            @PathVariable Long id,
            @RequestParam Long companyId,
            HttpServletRequest request) {
        // 验证用户是企业管理员
        // TODO: 验证用户是否为该企业的管理员
        
        boolean success = jobFairService.registerCompanyForJobFair(id, companyId);
        return Result.success(success);
    }
    
    /**
     * 获取招聘会参展企业列表
     */
    @GetMapping("/{id}/companies")
    public Result<List<CompanyVO>> getJobFairCompanies(@PathVariable Long id) {
        List<CompanyVO> companies = jobFairService.getJobFairCompanies(id);
        return Result.success(companies);
    }
    
    /**
     * 检查公司是否已经报名招聘会
     */
    @GetMapping("/{id}/company/check")
    public Result<Boolean> checkCompanyRegistered(
            @PathVariable Long id,
            @RequestParam Long companyId) {
        boolean registered = jobFairService.isCompanyRegistered(id, companyId);
        return Result.success(registered);
    }
    
    /**
     * 获取招聘会职位列表
     */
    @GetMapping("/{id}/jobs")
    public Result<List<JobVO>> getJobFairJobs(@PathVariable Long id) {
        List<JobVO> jobs = jobFairService.getJobFairJobs(id);
        return Result.success(jobs);
    }
    
    /**
     * 检查用户是否已报名招聘会
     */
    @GetMapping("/{id}/check-registration")
    public Result<Boolean> checkUserRegistered(@PathVariable Long id, HttpServletRequest request) {
        Long userId = jwtUtil.getUserIdFromRequest(request);
        boolean registered = jobFairService.isUserRegistered(id, userId);
        return Result.success(registered);
    }
} 